package com.ebs.md.container.esper;

/**
 * Revision Author:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/container/esper/AlertGroup.java $
 *  $Revision: #2 $
 *  $DateTime: 2012/10/03 09:41:26 $
 *  $Change: 115343 $
 *  $Author: Arik.Sher $
 */

import java.util.Set;

import org.springframework.util.Assert;

import com.ebs.md.fxmonitor.marketmonitor.config.INameableConfig;
import com.espertech.esper.client.EPAdministrator;

/**
 * Container for Statements for a specific Alert(such as PriceLaddering).
 *
 * @param <CONF>
 */
public class AlertGroup<CONF extends INameableConfig>
{
	protected boolean                   configRequired = true;
	protected CONF                      config;
	protected String                    region;
	protected Set<AbsStatement<CONF>>   statements;

	public void init() {
		if ( configRequired ) {
			Assert.notNull(config, "'config' should not be null");
		}
		Assert.hasText (region,     "'region' should contain text");
		Assert.notEmpty(statements, "'statements' should contain at least 1 element");
		
		initStatements();
	}

	public void setConfig(CONF config) {
		this.config = config;
	}

	public void setConfigRequired(boolean configRequired) {
		this.configRequired = configRequired;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public void setStatements(Set<AbsStatement<CONF>> statements) {
		this.statements = statements;
	}

	protected void initStatements() {
		for ( AbsStatement<CONF> curr : statements ) {
			curr.setConfigRequired(configRequired);
			curr.setConfig(config);
			curr.setRegion(region);
			curr.init();
		}
	}
	
	public void prepare(EPAdministrator epAdministrator) {
		prepareStatements(epAdministrator);
	}
	
	protected void prepareStatements(EPAdministrator epAdministrator) {
		for ( AbsStatement<CONF> curr : statements ) {
			curr.prepare(epAdministrator);
		}
	}
	
}
